package com.e3roid.examples;

import java.util.ArrayList;

import android.view.MotionEvent;
import android.widget.Toast;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Shape;
import com.e3roid.drawable.modifier.AlphaModifier;
import com.e3roid.drawable.modifier.ProgressModifier;
import com.e3roid.drawable.modifier.ShapeModifier;
import com.e3roid.drawable.modifier.function.Linear;
import com.e3roid.drawable.sprite.AnimatedSprite;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.event.ModifierEventListener;

public class AlphaModifierExample extends E3Activity implements ModifierEventListener {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private TiledTexture texture;
	private AnimatedSprite sprite;
	
	private boolean modifying = false;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.addEventListener(this);
		
		int centerX = (getWidth()  - texture.getTileWidth())  / 2;
		int centerY = (getHeight() - texture.getTileHeight()) / 2;
		
		// Add animation frames from tile.
		ArrayList<AnimatedSprite.Frame> frames = new ArrayList<AnimatedSprite.Frame>();
		frames.add(new AnimatedSprite.Frame(0, 0));
		frames.add(new AnimatedSprite.Frame(1, 0));
		frames.add(new AnimatedSprite.Frame(2, 0));
		frames.add(new AnimatedSprite.Frame(3, 0));
		
		// Start animation with 500msec, infinite loop.
		sprite = new AnimatedSprite(texture, centerX, centerY);
		sprite.animate(500, 0, frames);
		scene.getTopLayer().add(sprite);
		
		scene.setBackgroundColor(0.94f, 1.00f, 0.94f, 1);

		Toast.makeText(getContext(), "Touch screen to run modifiers.", Toast.LENGTH_LONG).show();
		return scene;
	}

	@Override
	public void onLoadResources() {
		// 31x49 pixel sprite with 1px border and (0,0) tile.
		texture = new TiledTexture("king.png", 31, 49, 0, 0, 3, 2, this);
	}
	
	@Override
	public boolean onSceneTouchEvent(E3Scene scene, MotionEvent motionEvent) {
		if (sprite != null && motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
			if (!modifying) {
				if (sprite.isTransparent()) {
					sprite.addModifier(new ProgressModifier(
							AlphaModifier.getFadeInModifier(sprite.getAlpha()),
							3000, Linear.getInstance(), AlphaModifierExample.this));
				} else {
					sprite.addModifier(new ProgressModifier(
							AlphaModifier.getFadeOutModifier(sprite.getAlpha()),
							3000, Linear.getInstance(), AlphaModifierExample.this));
				}
			}
		}
		return false;
	}

	@Override
	public void onModifierStart(ShapeModifier modifer, Shape shape) {
		this.modifying = true;
	}

	@Override
	public void onModifierFinished(ShapeModifier modifier, Shape shape) {
		this.modifying = false;
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(getContext(), "Modifier finished.", Toast.LENGTH_SHORT).show();
			}
		});
	}
	
}